<template>
  <div class="table1">
    <div class="btn-wrap">
      <a-button type="primary" style="margin-right: 15px;" @click="addFormVisible = true">添加</a-button>
      <a-button type="info" @click="download">导出</a-button>
    </div>
    <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="true" :loading="loading" :columns="columns"
      :dataSource="pageData" @selectedRow="onSelectedRow" @change="onTableChange">
      <!--操作-->
      <template slot="action" slot-scope="text, record">
        <a-button @click="onDetail(record)" type="link" style="padding:0;">查看</a-button>

        <a-button @click="onPeopleDetail(record)" type="link" style="padding:0;margin-left: 10px;">参与人员</a-button>
      </template>
      <template slot="status" slot-scope="text, record" v-for="(item, index) in headerData" >
        <div :key="index">
          <span style="color: red;" v-if="record.submitStatus == 1">未提交</span>
          <span style="color: blue;" v-if="record.submitStatus == 2">待审批</span>
          <span style="color: red;" v-if="record.submitStatus == 3">未通过</span>
          <span style="color: green;" v-if="record.submitStatus == 4">已通过</span>
        </div>
      </template>
    </z-table>
    <a-modal title="添加" :visible="addFormVisible" @ok="onAddSubmit" @cancel="onAddFormCancel">
      <a-form-model ref="addForm" :model="addForm" :label-col="{span: 7}" :wrapper-col="{span: 16}">
          <a-form-model-item label="组员名称" prop="memberId" :rules="{required: true, message: '请选择组员', trigger: ['change']}">
            <a-select v-model="addForm.memberId">
              <a-select-option :value="item.id" v-for="item in memberList" :key="id">{{item.memberName}}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="选择单元" prop="unitId" :rules="{required: true, message: '请选择单元', trigger: ['change']}">
            <a-select v-model="addForm.unitId">
              <a-select-option :value="item.id" v-for="item in unitList" :key="item.id">第{{item.unitOrder + 1}}单元</a-select-option>
            </a-select>
          </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
  import { memberList } from '@/api/sjsk/activity/group/detail/mature/table1'
  import { getList, add } from '@/api/sjsk/activity/group/detail/end/table1'
  import { unitList } from '@/api/sjsk/activity/common.js'
  import downloadXlsl from '../../downloadXlsx.js'
  export default {
    data() {
      return {
        loading:false,
        pageFilter: {
          pageNo: 1,
          pageSize: 10
        },
        pageData: {},
        form: {},
        headerData: [],
        memberList: [],
        unitList: [],
        addFormVisible: false,
        addForm: {
          "activityId": '',
          "unitId": '',
          memberId: '',
        }
      }
    },
    created() {
      this.getMemberList()
      this.getUnitList()
      this.onQuery()
      this.addForm.activityId = this.$route.query.id
    },
    computed: {
      columns: function() {
        let columns = [{
            title: '组员名称',
            dataIndex: 'memberName',
            width: 80,
            align: 'center',
            customRender: text => {
              return <j-ellipsis value={text} length={14} />
            }
        }]
        this.headerData.map(item => {
          columns.push({
            title: item.unitName,
            width: 80,
            dataIndex: item.unitId,
            align: 'center',
            scopedSlots: { customRender: 'status' }
          }) 
        })
        columns.push({
          title: '最新提交日期',
          align: 'center',
          dataIndex: 'lastModifiedDate',
        })
        columns.push({
          title: '操作',
          width: 120,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
        return columns
      }
    },
    methods: {
      download() {
        downloadXlsl(this.$route.query.id, 8)
      },
      getUnitList() {
        const activityId = this.$route.query.id
        unitList({activityId}).then(res => {
          this.unitList = res.result
        })
      },
      resetForm() {
        this.addForm = {
          "activityId": this.$route.query.id,
          "unitId": '',
          memberFeedback: '',
          goalProcess: '',
          memberAnalysis: '',
          workIntrospection: '',
          nextUnitWorking: ''
        }
      },
      onAddSubmit() {
        this.$refs.addForm.validate().then(res => {
          add(this.addForm).then(res => {
            this.$message.success('添加成功')
            this.addFormVisible = false
            this.resetForm()
          }).catch(err => {
            this.$message.error(err.message)
          })
        })
      },
      onAddFormCancel() {
        this.addFormVisible = false
        this.resetForm()
      },
      getMemberList() {
        memberList({activityId: this.$route.query.id}).then(res => {
          if(res.success) {
            this.memberList = res.result.records
          }
        })
      },
      onQuery() {
        this.pageFilter.pageNo = 1
        this.refreshTable()
      },
      onSelectedRow(keys) {
        this.selectKeys = keys
        this.isDisabled = keys.length > 0 ? false : true
      },
      onTableChange(pagination, pageFilter, sorter) {
        let keyMap = {
          currentPage: 'pageNo'
        }
        let paginations = Object.keys(pagination).reduce((newData, key) => {
          let newKey = keyMap[key] || key
          newData[newKey] = pagination[key]
          return newData
        }, {})
        this.pageFilter = Object.assign({}, this.pageFilter, paginations)
        this.refreshTable()
      },
      refreshTable() {
        let form = Object.assign({}, this.queryParam, this.pageFilter)
        form.activityId = this.$route.query.id
        this.loading = true
        getList(form).then((res) => {
            if (res.success) {
              res.result.records.map(item => {
                item.submits.map(child => {
                  item[child.unitId] = child.submitStatus
                })
              })
              this.pageData = res.result
              if(res.result.records && res.result.records.length > 0) {
                this.headerData = res.result.record[0].submits
              }
              
              
              this.loading = false
              this.$refs.table.onClearRowKeys()
            } else {
              this.loading = false
            }
          })
          .catch((e) => {
            this.loading = false
            this.$message.warn('请求失败！')
          })
      },
    }
  }
</script>

<style lang="less" scoped>
  .table1 {
    box-sizing: border-box;
    padding: 20px 30px;
    .btn-wrap{
      margin-bottom: 20px;
    }
  }
</style>
